//
//  BaseNavigationVC.m
//  GasCome
//
//  Created by zfc on 2020/1/7.
//  Copyright © 2020 zfc. All rights reserved.
//

#import "BaseNavigationVC.h"

@interface BaseNavigationVC () <UINavigationControllerDelegate>

@property (nonatomic, assign) BOOL pushing;

@end

@implementation BaseNavigationVC

- (void)viewDidLoad {
    [super viewDidLoad];

    self.pushing = NO;
    self.delegate = self;

    self.navigationBar.translucent = NO;
    [self.navigationBar setBarTintColor:[UIColor whiteColor]];
    [self.navigationBar setBackgroundImage:[UIImage imageNamed:@"common_line"] forBarPosition:UIBarPositionBottom barMetrics:UIBarMetricsDefault];
    self.navigationBar.shadowImage = [UIImage imageNamed:@"common_line"];
}

- (void) backAction {
    [self popViewControllerAnimated:YES];
}

//重写这个方法，在跳转后自动隐藏tabbar
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if ([self.viewControllers count] > 0){
        viewController.hidesBottomBarWhenPushed = YES;
        //可以在这里定义返回按钮等
        UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"common_ic_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
        backItem.imageInsets = UIEdgeInsetsMake(0, 0, 0, 0);
        viewController.navigationItem.leftBarButtonItem = backItem;
    }

    if (self.pushing == YES) {
        return;
    }else {
        self.pushing = YES;
    }

    //一定要写在最后，要不然无效
    [super pushViewController:viewController animated:animated];
    //处理了push后隐藏底部UITabBar的情况，并解决了iPhonX上push时UITabBar上移的问题。
    CGRect rect = self.tabBarController.tabBar.frame;
    rect.origin.y = [UIScreen mainScreen].bounds.size.height - rect.size.height;
    self.tabBarController.tabBar.frame = rect;
}

#pragma mark - UINavigationControllerDelegate
-(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    self.pushing = NO; //完成PUSH
}

@end
